<?php 
require_once 'api.php';
$request=$_SERVER['QUERY_STRING'];
$recent_url=dirname('http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]);
$mchid = 'a0e5b19a8b4047c88184412997a421d1';
$private_key = 'a0c76773b8ca44ac9fa5100f5675c95f';
parse_str($request,$array);
try {    
    //创建订单支付编号
    $data=array(
            'mchid'     	=> $mchid,
            'out_trade_no'	=> time(),
            'type'  		=> $array['type'],
            'total_fee' 	=> $array['amount']*100,
            'body'  		=> '支付测试',
            'notify_url'	=> $recent_url.'/notify.php',
            'nonce_str' 	=> str_shuffle(time())
        );
		$url ='https://admin.xunhuweb.com/pay/payment';
        if($data['type']=='wechat'){
	        if(XH_Payment_Api::is_wechat_app()){
	         	 $data['redirect_url']=$recent_url.'/return.php';
	        	 $data['sign']     = XH_Payment_Api::generate_xh_hash($data,$private_key);
	        	 $pay_url     = XH_Payment_Api::data_link('https://admin.xunhuweb.com/pay/cashier', $data);
	        	 header("Location:". htmlspecialchars_decode($pay_url,ENT_NOQUOTES));
		    	 exit;
	        }
        	if(XH_Payment_Api::is_app_client()){
	        	 $data['trade_type']='WAP';
	        	 $data['sign']     = XH_Payment_Api::generate_xh_hash($data,$private_key);
		         $response   	  = XH_Payment_Api::http_post_json($url, json_encode($data));
		         $result     	  = $response?json_decode($response,true):null;
	        if(!$result){
	            throw new Exception('Internal server error',500);
	        }
	        $sign       	  = XH_Payment_Api::generate_xh_hash($result,$private_key);
	        if(!isset( $result['sign'])|| $sign!=$result['sign']){
	            throw new Exception($result['err_msg'],$result['err_code']);
	        }
	        $url =$result['mweb_url'];
	        header("Location:".$url);
	        exit;
        	}
	        $data['sign']     = XH_Payment_Api::generate_xh_hash($data,$private_key);
	        $response   	  = XH_Payment_Api::http_post_json($url, json_encode($data));
	        $result     	  = $response?json_decode($response,true):null;
	        if(!$result){
	            throw new Exception('Internal server error',500);
	        }
	        $sign       	  = XH_Payment_Api::generate_xh_hash($result,$private_key);
	        if(!isset( $result['sign'])|| $sign!=$result['sign']){
	            throw new Exception($result['err_msg'],$result['err_code']);
	        }
	        $url =$result['code_url'];
        	 ?>
			<!DOCTYPE html>
			<html>
			<head>
		    <meta charset="utf-8">
		    <meta http-equiv="X-UA-Compatible" content="IE=edge">
		    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		    <meta name="keywords" content="">
		    <meta name="description" content="">   
		    <title>微信支付收银台</title>
		    <style>
		         *{margin:0;padding:0;}
		          body{background: #f2f2f4;}
		         .clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
		        .clearfix { display: inline-block; }
		        * html .clearfix { height: 1%; }
		        .clearfix { display: block; }
		          .xh-title{height:75px;line-height:75px;text-align:center;font-size:30px;font-weight:300;border-bottom:2px solid #eee;background: #fff;}
		          .qrbox{max-width: 900px;margin: 0 auto;padding:85px 20px 20px 50px;}
		          
		          .qrbox .left{width: 40%;
		            float: left;    
		             display: block;
		            margin: 0px auto;}
		          .qrbox .left .qrcon{
		            border-radius: 10px;
		            background: #fff;
		            overflow: visible;
		            text-align: center;
		            padding-top:25px;
		            color: #555;
		            box-shadow: 0 3px 3px 0 rgba(0, 0, 0, .05);
		            vertical-align: top;
		            -webkit-transition: all .2s linear;
		            transition: all .2s linear;
		          }
		            .qrbox .left .qrcon .logo{width: 100%;}
		            .qrbox .left .qrcon .title{font-size: 16px;margin: 10px auto;width: 100%;}
		            .qrbox .left .qrcon .price{font-size: 22px;margin: 0px auto;width: 100%;}
		            .qrbox .left .qrcon .bottom{border-radius: 0 0 10px 10px;
		            width: 100%;
		            background: #32343d;
		            color: #f2f2f2;padding:15px 0px;text-align: center;font-size: 14px;}
		           .qrbox .sys{width: 60%;float: right;text-align: center;padding-top:20px;font-size: 12px;color: #ccc}
		           .qrbox img{max-width: 100%;}
		           @media (max-width : 767px){
		        .qrbox{padding:20px;}
		            .qrbox .left{width: 90%;float: none;}   
		            .qrbox .sys{display: none;}
		           }
		           
		           @media (max-width : 320px){
		
		          }
		          @media ( min-width: 321px) and ( max-width:375px ){
		
		          }
		    </style>
		    </head>
		    
		    <body>
		     <div class="xh-title">微信支付收银台</div>
		      <div class="qrbox clearfix">
		      <div class="left">
		         <div class="qrcon">
		           <h5><img src="<?php print $recent_url ?>/images/wechat/logo.png" alt=""></h5>
		             <div class="title">支付测试-￥<?php echo $array['amount'] ?></div>
		             <div class="price">￥<?php echo $array['amount']?></div>
		             <div align="center"><div id="wechat_qrcode" style="width: 250px;height: 250px;"></div></div>
		             <div class="bottom">
		             <?php 
		             if(XH_Payment_Api::is_app_client()){
		                 ?>步骤1：截图保存到手机相册<br/>步骤2：微信扫一扫选择相册(右上角)完成扫描
		                 <?php 
		             }else{
		                 ?>
		                 	请使用微信扫一扫<br/>
		    				扫描二维码支付
		                 <?php 
		             }
		             ?>
		             	
		             </div>
		         </div>
		         
		  </div>
		     <div class="sys"><img src="<?php print $recent_url ?>/images/wechat/wechat-sys.png" alt=""></div>
		  </div>
		     <script src="js/jquery-2.1.4.min.js"></script>
		      <script src="js/qrcode.js"></script>
			 <script src="js/qrcode.min.js"></script>
		     <script type="text/javascript">
		     (function($){
		    		window.view={
						query:function () {
					        $.ajax({
					            type: "POST",
					            url: "<?php echo $recent_url.'/query.php?out_trade_no='.$data['out_trade_no'] ?>",
					            timeout:6000,
					            cache:false,
					            dataType:'text',
					            success:function(e){
					            		if (e && e.indexOf('complete')!==-1) {
		    			                $('#weixin-notice').css('color','green').text('已支付成功，跳转中...');
		    		                    window.location.href = "<?php echo $recent_url.'/return.php' ?>";
		    		                    return;
		    		                }
					                setTimeout(function(){window.view.query();}, 2000);
					            },
					            error:function(){
					            	 setTimeout(function(){window.view.query();}, 2000);
					            }
					        });
					    }
		    		};
		    		var qrcode = new QRCode(document.getElementById("wechat_qrcode"), {
		              width : 220,
		              height : 220
		            });
		            
		            <?php if(!empty($url)){
		              ?>
		              qrcode.makeCode("<?php print $url?>");
		              window.view.query();
		            <?php 
		            }?>
		    		
		    	})(jQuery);
		    	</script>
			</body>
		</html>
			<?php  
        }else{
        	$data['sign']	  = XH_Payment_Api::generate_xh_hash($data,$private_key);
        	 if(XH_Payment_Api::is_app_client()){
	        	$pay_url     = XH_Payment_Api::data_link('https://admin.xunhuweb.com/alipaycashier', $data);
	        	 header("Location:". htmlspecialchars_decode($pay_url,ENT_NOQUOTES));
		    	 exit;
	        }
        	$response   	  = XH_Payment_Api::http_post_json($url, json_encode($data));
	        $result     	  = $response?json_decode($response,true):null;
	        if(!$result){
	            throw new Exception('Internal server error',500);
	        }
	        $sign       	  = XH_Payment_Api::generate_xh_hash($result,$private_key);
	        if(!isset( $result['sign'])|| $sign!=$result['sign']){
	            throw new Exception($result['err_msg'],$result['err_code']);
	        }
	        $url =$result['code_url'];
        		 ?>
			<!DOCTYPE html>
			<html>
			<head>
		    <meta charset="utf-8">
		    <meta http-equiv="X-UA-Compatible" content="IE=edge">
		    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		    <meta name="keywords" content="">
		    <meta name="description" content="">   
		    <title>支付宝收银台</title>
		    <style>
		         *{margin:0;padding:0;}
		          body{background: #f2f2f4;}
		         .clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
		        .clearfix { display: inline-block; }
		        * html .clearfix { height: 1%; }
		        .clearfix { display: block; }
		          .xh-title{height:75px;line-height:75px;text-align:center;font-size:30px;font-weight:300;border-bottom:2px solid #eee;background: #fff;}
		          .qrbox{max-width: 900px;margin: 0 auto;padding:85px 20px 20px 50px;}
		          
		          .qrbox .left{width: 40%;
		            float: left;    
		             display: block;
		            margin: 0px auto;}
		          .qrbox .left .qrcon{
		            border-radius: 10px;
		            background: #fff;
		            overflow: visible;
		            text-align: center;
		            padding-top:25px;
		            color: #555;
		            box-shadow: 0 3px 3px 0 rgba(0, 0, 0, .05);
		            vertical-align: top;
		            -webkit-transition: all .2s linear;
		            transition: all .2s linear;
		          }
		            .qrbox .left .qrcon .logo{width: 100%;}
		            .qrbox .left .qrcon .title{font-size: 16px;margin: 10px auto;width: 100%;}
		            .qrbox .left .qrcon .price{font-size: 22px;margin: 0px auto;width: 100%;}
		            .qrbox .left .qrcon .bottom{border-radius: 0 0 10px 10px;
		            width: 100%;
		            background: #32343d;
		            color: #f2f2f2;padding:15px 0px;text-align: center;font-size: 14px;}
		           .qrbox .sys{width: 60%;float: right;text-align: center;padding-top:20px;font-size: 12px;color: #ccc}
		           .qrbox img{max-width: 100%;}
		           @media (max-width : 767px){
		        .qrbox{padding:20px;}
		            .qrbox .left{width: 90%;float: none;}   
		            .qrbox .sys{display: none;}
		           }
		           
		           @media (max-width : 320px){
		
		          }
		          @media ( min-width: 321px) and ( max-width:375px ){
		
		          }
		    </style>
		    </head>
		    
		    <body>
		     <div class="xh-title">支付宝收银台</div>
		      <div class="qrbox clearfix">
		      <div class="left">
		         <div class="qrcon">
		           <h5><img src="<?php print $recent_url ?>/images/alipay/logo.png" alt=""></h5>
		             <div class="title">支付测试-￥<?php echo $array['amount'] ?></div>
		             <div class="price">￥<?php echo $array['amount']?></div>
		             <div align="center"><div id="wechat_qrcode" style="width: 250px;height: 250px;"></div></div>
		             <div class="bottom">
		             <?php
	             	 if(XH_Payment_Api::is_app_client()){
	                 ?>
	                 步骤1：长按二维码保存到手机相册<br/>步骤2：微信扫一扫选择相册(右上角)完成扫描
	                 <?php 
		             }else{
		             	?>
		                 	请使用支付宝扫一扫<br/>
		    				扫描二维码支付
		    				     <?php 
		             }
		             ?>
		             </div>
		         </div>
		         
		  </div>
		     <div class="sys"><img src="<?php print $recent_url ?>/images/alipay/alipay-sys.png" alt=""></div>
		  </div>
		     <script src="js/jquery-2.1.4.min.js"></script>
		      <script src="js/qrcode.js"></script>
			 <script src="js/qrcode.min.js"></script>
		     <script type="text/javascript">
		     (function($){
		    		window.view={
						query:function () {
					        $.ajax({
					            type: "POST",
					            url: "<?php echo $recent_url.'/query.php?out_trade_no='.$data['out_trade_no'] ?>",
					            timeout:6000,
					            cache:false,
					            dataType:'text',
					            success:function(e){
					            		if (e && e.indexOf('complete')!==-1) {
		    			                $('#weixin-notice').css('color','green').text('已支付成功，跳转中...');
		    		                    window.location.href = "<?php echo $recent_url.'/return.php' ?>";
		    		                    return;
		    		                }
					                setTimeout(function(){window.view.query();}, 2000);
					            },
					            error:function(){
					            	 setTimeout(function(){window.view.query();}, 2000);
					            }
					        });
					    }
		    		};
		    		var qrcode = new QRCode(document.getElementById("wechat_qrcode"), {
		              width : 220,
		              height : 220
		            });
		            <?php if(!empty($url)){
		              ?>
		              qrcode.makeCode("<?php print $url?>");
		              window.view.query();
		            <?php 
		            }?>
		    		 setTimeout(function(){window.view.query();}, 2000);
		    	})(jQuery);
		    	</script>
			</body>
		</html>
			<?php
        }
		  
    } catch (Exception $e) {
      exit;
}

?>